package com.joven.shelltest.utils.mybatisplusdim;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author wzj
 * @Date 2023-10-10 16:44
 **/
public class AnalyzerUtil {
    public static List<String> ikSegmentationList(String str, Integer length) {
        List<String> list = new LinkedList<>();
        try {
            if (StrUtil.isEmpty(str)) {
                return ListUtil.empty();
            }
            StringReader stringReader = new StringReader(str);
            IKSegmenter ik = new IKSegmenter(stringReader, false);
            Lexeme le;
            while ((le = ik.next()) != null) {
                String lexemeText = le.getLexemeText();
                if (lexemeText.length() >= length) {
                    list.add(lexemeText);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 部分分词
     *
     * @param str    str
     * @param length length
     * @return List<String>
     */
    public static List<String> partSegmentationList(String str, Integer length) {
        List<String> list = new ArrayList<>();
        if (StrUtil.isEmpty(str)) {
            return ListUtil.empty();
        }
        int strLength = str.length();
        for (int startIndex = 0; startIndex <= strLength - length; startIndex++) {
            String substring = str.substring(startIndex, startIndex + length);
            list.add(substring);
        }
        return list;
    }
}
